package com.youkeyi.ddy.cloud.basic.pub.ad.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.ad.entity.SysAdArea;
import com.youkeyi.ddy.cloud.basic.pub.ad.entity.SysAdCity;
import com.youkeyi.ddy.cloud.basic.pub.ad.entity.SysAdProvince;
import com.youkeyi.ddy.cloud.basic.pub.ad.service.SysAdAreaService;
import com.youkeyi.ddy.cloud.basic.pub.ad.service.SysAdCityService;
import com.youkeyi.ddy.cloud.basic.pub.ad.service.SysAdProvinceService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.PublicAdCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.dto.*;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.param.PublicAdAreaQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.param.PublicAdCityQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.ad.param.PublicAdProvinceQueryCloudParam;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessAdController
 *
 * @Author liuxiawang
 * @Date 2024/4/1 9:50 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessAdController implements PublicAdCloudService {

    @Autowired
    private SysAdProvinceService sysAdProvinceService;
    @Autowired
    private SysAdCityService sysAdCityService;
    @Autowired
    private SysAdAreaService sysAdAreaService;

    @Override
    public PublicAdProvinceListCloudDto queryProvince(PublicAdProvinceQueryCloudParam param) {
        PublicAdProvinceListCloudDto dto = new PublicAdProvinceListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getProvinceCodes())) {
            param.getProvinceCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysAdProvince> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getProvinceCodes()), SysAdProvince::getProvinceCode, param.getProvinceCodes())
                .like(StringUtils.isNotBlank(param.getProvinceNameFuzzy()), SysAdProvince::getProvinceName, param.getProvinceNameFuzzy())
        ;

        Page<SysAdProvince> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysAdProvince> provinceList = sysAdProvinceService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(provinceList.getRecords())) {
            dto.setTotal(provinceList.getTotal());

            List<PublicAdProvinceListItemCloudDto> items = new ArrayList<>();
            provinceList.getRecords().forEach(province -> {
                PublicAdProvinceListItemCloudDto item = new PublicAdProvinceListItemCloudDto();
                item.setProvinceId(province.getProvinceId());
                item.setProvinceCode(province.getProvinceCode());
                item.setProvinceName(province.getProvinceName());
                item.setShortName(province.getShortName());
                item.setLng(province.getLng());
                item.setLat(province.getLat());
                item.setSort(province.getSort());
                item.setGmtCreate(province.getGmtCreate() != null ? province.getGmtCreate().getTime() : null);
                item.setGmtModified(province.getGmtModified() != null ? province.getGmtModified().getTime() : null);
                item.setMemo(province.getMemo());
                item.setDataState(province.getDataState());
                item.setTenantCode(province.getTenantCode());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public PublicAdCityListCloudDto queryCity(PublicAdCityQueryCloudParam param) {
        PublicAdCityListCloudDto dto = new PublicAdCityListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getProvinceCodes())) {
            param.getProvinceCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCityCodes())) {
            param.getCityCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysAdCity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getProvinceCodes()), SysAdCity::getProvinceCode, param.getProvinceCodes())
                .in(CollectionUtils.isNotEmpty(param.getCityCodes()), SysAdCity::getCityCode, param.getCityCodes())
                .like(StringUtils.isNotBlank(param.getCityNameFuzzy()), SysAdCity::getCityName, param.getCityNameFuzzy())
        ;

        Page<SysAdCity> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysAdCity> cityList = sysAdCityService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(cityList.getRecords())) {
            dto.setTotal(cityList.getTotal());

            List<PublicAdCityListItemCloudDto> items = new ArrayList<>();
            cityList.getRecords().forEach(city -> {
                PublicAdCityListItemCloudDto item = new PublicAdCityListItemCloudDto();
                item.setCityId(city.getCityId());
                item.setCityCode(city.getCityCode());
                item.setCityName(city.getCityName());
                item.setProvinceCode(city.getProvinceCode());
                item.setShortName(city.getShortName());
                item.setLng(city.getLng());
                item.setLat(city.getLat());
                item.setSort(city.getSort());
                item.setGmtCreate(city.getGmtCreate() != null ? city.getGmtCreate().getTime() : null);
                item.setGmtModified(city.getGmtModified() != null ? city.getGmtModified().getTime() : null);
                item.setMemo(city.getMemo());
                item.setDataState(city.getDataState());
                item.setTenantCode(city.getTenantCode());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public PublicAdAreaListCloudDto queryArea(PublicAdAreaQueryCloudParam param) {
        PublicAdAreaListCloudDto dto = new PublicAdAreaListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getAreaCodes())) {
            param.getAreaCodes().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCityCodes())) {
            param.getCityCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysAdArea> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCityCodes()), SysAdArea::getCityCode, param.getCityCodes())
                .in(CollectionUtils.isNotEmpty(param.getAreaCodes()), SysAdArea::getAreaCode, param.getAreaCodes())
                .like(StringUtils.isNotBlank(param.getAreaNameFuzzy()), SysAdArea::getAreaName, param.getAreaNameFuzzy())
        ;

        Page<SysAdArea> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysAdArea> areaList = sysAdAreaService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(areaList.getRecords())) {
            dto.setTotal(areaList.getTotal());

            List<PublicAdAreaListItemCloudDto> items = new ArrayList<>();
            areaList.getRecords().forEach(area -> {
                PublicAdAreaListItemCloudDto item = new PublicAdAreaListItemCloudDto();
                item.setAreaId(area.getAreaId());
                item.setAreaCode(area.getAreaCode());
                item.setCityCode(area.getCityCode());
                item.setAreaName(area.getAreaName());
                item.setShortName(area.getShortName());
                item.setLng(area.getLng());
                item.setLat(area.getLat());
                item.setSort(area.getSort());
                item.setGmtCreate(area.getGmtCreate() != null ? area.getGmtCreate().getTime() : null);
                item.setGmtModified(area.getGmtModified() != null ? area.getGmtModified().getTime() : null);
                item.setMemo(area.getMemo());
                item.setDataState(area.getDataState());
                item.setTenantCode(area.getTenantCode());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }
}
